﻿<%@ WebHandler Language="C#" Class="list" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.Script.Serialization;
using System.Web.SessionState;
using System.Web.Services;

public class list : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    private StringBuilder sb = null;
    public DataSet ds = null;
    public DataTable dt = null;
    private string sqlwhere = "";
    public void ProcessRequest (HttpContext context) {
        string type = PageBase.ext_string(context.Request.Params["type"]);

        switch (type)
        {
            //获取订单列表
            case "getlist":
                getinfo_list(context);
                break;

            //查询制单人
            case "get_zdr":
                getinfo_zdr(context);
                break;

            //查询制单人
            case "getlist_lcbh":
                getinfo_list_mx(context);
                break;

            default:
                //getinfo(context);
                break;
        }
    }

    //获取明细
    public void getinfo_list_mx(HttpContext context)
    {
        //页数1
        int page = PageBase.ext_int(context.Request.Params["page"]);
        //每页条数10
        int limit =PageBase.ext_int(context.Request.Params["limit"]);
        //排序字段
        string field =PageBase.ext_string(context.Request.Params["field"]);
        //排序方式
        string order = PageBase.ext_string(context.Request.Params["order"]);

        string lcbh = PageBase.ext_string(context.Request.Params["lcbh"]);
        string orderby = "id ";
        string where = @"select COUNT(*) OVER(PARTITION BY 1) AS TOTAL,id,付款方式,科目类型,制单人,凭证字,辅助核,报销部门编码,报销部门,报销人编码,
        报销人名称,科目编码,科目名称,借,贷,摘要 from LIK_TO202.satchioa.dbo.A_U8_GLaccvouch  where 流程编号='"+lcbh+"'";

        if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(field))
        {
            orderby =field + " " + order;
        }
        DataTable dt =  DBHelper.Pagination2(where, limit, page, orderby, "");
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        int count = 0;
        if(dt.Rows.Count>0)
            count = PageBase.ext_int(dt.Rows[0]["TOTAL"].ToString());

        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"付款方式\":\"" + dt.Rows[i]["付款方式"].ToString() + "\"");
            sb.Append(",\"科目类型\":\"" + dt.Rows[i]["科目类型"] + "\"");
            sb.Append(",\"制单人\":\"" + dt.Rows[i]["制单人"] + "\"");
            sb.Append(",\"凭证字\":\"" + dt.Rows[i]["凭证字"] + "\"");
            sb.Append(",\"辅助核\":\"" + dt.Rows[i]["辅助核"] + "\"");
            sb.Append(",\"报销部门编码\":\"" + dt.Rows[i]["报销部门编码"] + "\"");
            sb.Append(",\"报销部门\":\"" + dt.Rows[i]["报销部门"] + "\"");
            sb.Append(",\"报销人编码\":\"" + dt.Rows[i]["报销人编码"] + "\"");
            sb.Append(",\"报销人名称\":\"" + dt.Rows[i]["报销人名称"] + "\"");
            sb.Append(",\"科目编码\":\"" + dt.Rows[i]["科目编码"] + "\"");
            sb.Append(",\"科目名称\":\"" + dt.Rows[i]["科目名称"] + "\"");
            sb.Append(",\"借\":\"" + dt.Rows[i]["借"] + "\"");
            sb.Append(",\"贷\":\"" + dt.Rows[i]["贷"] + "\"");
            sb.Append(",\"摘要\":\"" + dt.Rows[i]["摘要"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //查询制单人
    public void getinfo_zdr(HttpContext context)
    {
        string sql = @"select 制单人 as zdr from LIK_TO202.satchioa.dbo.A_U8_GLaccvouch a
left join LIK_TO202.satchioa.dbo.hrmresource b on a.制单人=b.lastname
 where b.loginid<>'' and a.制单人<>'冯良德' and 报销日期>(select dateAdd(yy,-1,getdate())) and 制单人 is not null group by 制单人";
        DataTable dt = DBHelper.ExecuteDataTable(sql);

        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        sb.Append("{\"code\":\"" + code + "\",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"zdr\":\"" + dt.Rows[i]["zdr"].ToString() + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }

    //获取简历列表
    public void getinfo_list(HttpContext context)
    {
        //页数1
        int page = PageBase.ext_int(context.Request.Params["page"]);
        //每页条数10
        int limit =PageBase.ext_int(context.Request.Params["limit"]);
        //排序字段
        string field =PageBase.ext_string(context.Request.Params["field"]);
        //排序方式
        string order = PageBase.ext_string(context.Request.Params["order"]);


        string orderby = "bxrq desc";
        string where = @"select distinct 报销日期 bxrq,流程名称 lcmc,流程编号 lcbh,制单人 zdr,rtrim(ltrim(readstatus)) readstatus,readtime,readerror from LIK_TO202.satchioa.dbo.A_U8_GLaccvouch where 1=1 
                        ";

        string lcbh =PageBase.ext_string(context.Request.Params["lcbh"]);
        string zdr =PageBase.ext_string(context.Request.Params["zdr"]);
        string ksrq =PageBase.ext_string(context.Request.Params["ksrq"]);
        string jsrq =PageBase.ext_string(context.Request.Params["jsrq"]);
        string status =PageBase.ext_string(context.Request.Params["status"]);
        if (status == "0")
            status = "成功";
        else if (status == "1")
            status = "失败";


        //流程编号
        if (!string.IsNullOrEmpty(lcbh))
        {
            where += " and 流程编号 like N'%" + lcbh + "%'";
        }
        //制单人
        if (!string.IsNullOrEmpty(zdr))
        {
            where += " and 制单人 ='" + zdr + "'";
        }
        //报销开始日期
        if (!string.IsNullOrEmpty(ksrq))
        {
            where += " and 报销日期 >='" + ksrq + "'";
        }
        //报销结束日期
        if (!string.IsNullOrEmpty(jsrq))
        {
            where += " and 报销日期<= '" + jsrq + "'";
        }
        //状态
        if (!string.IsNullOrEmpty(status))
        {
            where += " and readstatus= '" + status + "'";
        }

        if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(field))
        {
            orderby =field + " " + order;
        }
        DataTable dt =  DBHelper.Pagination2(where, limit, page, orderby, "");
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        int count = 0;
        if(dt.Rows.Count>0)
            count = PageBase.ext_int(dt.Rows[0]["TOTAL"].ToString());

        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"bxrq\":\"" + dt.Rows[i]["bxrq"].ToString() + "\"");
            sb.Append(",\"lcmc\":\"" + dt.Rows[i]["lcmc"] + "\"");
            sb.Append(",\"lcbh\":\"" + dt.Rows[i]["lcbh"] + "\"");
            sb.Append(",\"zdr\":\"" + dt.Rows[i]["zdr"] + "\"");
            sb.Append(",\"readstatus\":\"" + dt.Rows[i]["readstatus"] + "\"");
            sb.Append(",\"readtime\":\"" + dt.Rows[i]["readtime"] + "\"");
            sb.Append(",\"readerror\":\"" + dt.Rows[i]["readerror"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }


    public bool IsReusable {
        get {
            return false;
        }
    }

}